﻿using System;
using System.Globalization;
using System.Web.UI.WebControls;

namespace UserControl
{
    public partial class UserControlPage : System.Web.UI.UserControl
    {
        public int CurrentPage { get; set; } //Page currently requested
        public int VisiblePages { get; set; } //Number pages visible
        public int TotalPages { get; set; } //Total page posible
        public string NavigatePage { get; set; } //Navigation url
        public string CssClass { get; set; } //Css per page
   
        protected void Page_Load(object sender, EventArgs e)
        {
            if(TotalPages < 2)
            {
                return;
            }
            if (VisiblePages < 1 || VisiblePages > 30)
            {
                throw new Exception("Visible pages must be in range {1-30}");
            }

            if (NavigatePage == null)
            {
                throw new Exception("Navigate page must be specified");
            }

            if (CurrentPage <= 0 || CurrentPage > TotalPages)
            {
                throw new Exception("Current page is incorrect");
            }

            if (NavigatePage.Contains("?"))
            {
                NavigatePage = NavigatePage + "&page=";
            }
            else
            {
                NavigatePage = NavigatePage + "?page=";
            }

            //Create first page
            var firstLink = new HyperLink {Text = "<<", NavigateUrl = NavigatePage + 1, ToolTip = "Đi đến trang đầu"};
            Controls.Add(firstLink);
            //Create previous page
            var previousLink = new HyperLink {Text = "<"};
            int previous = (CurrentPage == 1) ? 1 : CurrentPage - 1;
            previousLink.NavigateUrl = NavigatePage + previous;
            previousLink.ToolTip = "Đi đến trang trước";
            Controls.Add(previousLink);

            int side = VisiblePages / 2;
            int step = CurrentPage - side;
            step = step <= 0 ? 0 : step - 1;
            for (int i = 1; i <= VisiblePages; i++)
            {
                int pageIndex = i + step;
                if (pageIndex > TotalPages)
                {
                    break;
                }
                var pageLink = new HyperLink {Text = pageIndex.ToString(CultureInfo.InvariantCulture)};
                if (pageIndex == CurrentPage)
                {
                    pageLink.Attributes["style"] = "color:red;font-weight:Bolder;";
                }
                pageLink.NavigateUrl = NavigatePage + pageIndex;
                pageLink.ToolTip = "Đi đến trang " + pageIndex;
                Controls.Add(pageLink);
            }

            //Create next page
            var nextLink = new HyperLink {Text = ">"};
            int next = (CurrentPage == TotalPages) ? TotalPages : CurrentPage + 1;
            nextLink.NavigateUrl = NavigatePage + next;
            nextLink.ToolTip = "Đi đến trang sau";
            Controls.Add(nextLink);
            //Create last page
            var lastLink = new HyperLink
                               {Text = ">>", NavigateUrl = NavigatePage + TotalPages, ToolTip = "Đi đến trang cuối"};
            Controls.Add(lastLink);

            //Set default style
            foreach (WebControl control in Controls)
            {
                control.Attributes["style"] += "padding:10px;";
            }

            //Set style manual
            if (CssClass != null)
            {
                foreach (WebControl control in Controls)
                {
                    control.CssClass += CssClass;
                }
            }
        }
    }
}